package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.vo.PageBean;

public class PersonDao extends GenericDao<Person>{
	@Override
	public Person handle(ResultSet rs) throws SQLException {	
		Person p =new Person();
		Family f = new Family();
		p.setYbcardno(rs.getString("ybcardno"));
		f.setFamilycode(rs.getString("familycode"));
		f=f.query();
		p.setFamily(f);
		p.setHnbh(rs.getString("hnbh"));
		p.setFullname(rs.getString("fullname"));
		p.setIdcard(rs.getString("idcard"));
		p.setRelative(rs.getString("relative"));
		p.setSex(rs.getString("sex"));
		p.setBrithday(rs.getDate("brithday"));
		p.setNation(rs.getString("nation"));
		p.setHealth(rs.getString("health"));
		p.setCulture(rs.getString("culture"));
		p.setAttribute(rs.getString("attribute"));
		p.setFarmer(rs.getString("farmer"));
		p.setMarry(rs.getString("marry"));
		p.setWorkunit(rs.getString("workunit"));
		p.setTel(rs.getString("tel"));
		return p;
	}
	
	public void add(Person p,Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try {
			String sql = "insert into person(ybcardno,familycode,hnbh,fullname,idcard,relative,sex,brithday,nation,health,culture,attribute,farmer,marry,workunit,tel) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			conn.setAutoCommit(false);
			int index=1;
			pstmt.setString(index++, p.getYbcardno());
			pstmt.setString(index++, p.getFamily().getFamilycode());
			pstmt.setString(index++, p.getHnbh());
			pstmt.setString(index++, p.getFullname());
			pstmt.setString(index++, p.getIdcard());
			pstmt.setString(index++, p.getRelative());
			pstmt.setString(index++, p.getSex());
			pstmt.setDate(index++, new java.sql.Date(p.getBrithday().getTime()));
			pstmt.setString(index++, p.getNation());
			pstmt.setString(index++, p.getHealth());
			pstmt.setString(index++, p.getCulture());
			pstmt.setString(index++, p.getAttribute());
			pstmt.setString(index++, p.getFarmer());
			pstmt.setString(index++, p.getMarry());
			pstmt.setString(index++, p.getWorkunit());
			pstmt.setString(index++, p.getTel());
			pstmt.executeUpdate();
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw new SQLException("添加参合农民档案失败 " + e.getMessage(), e);
		}finally{
			pstmt.close();
			conn.close();
		}
	}
	public void del(String idcard)throws SQLException{
		try {
			DbUtil db = new DbUtil();
			String sql = "delete from person where idcard='" + idcard + "'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("查询参合农民档案失败" + e.getMessage(), e);
		}
	}
	public Person query(String ybcardno)throws SQLException{
		String sql ="select * from person where ybcardno=?";
		Object[] params = {ybcardno};
		List<Person> list = new ArrayList<Person>();
		list = super.queryAll(sql, params);
		if(list.size()!=0)
			return list.get(0);
		else
			return null;
	}	
	public Person queryByIdcard(String idcard)throws SQLException{
		String sql ="select * from person where idcard=?";
		Object[] params = {idcard};
		List<Person> list = new ArrayList<Person>();
		list = super.queryAll(sql, params);
		if(list.size()!=0)
			return list.get(0);
		else
			return null;
	}	
	public PageBean queryByIdcard(String idcard,int page,int size)throws SQLException{
		String sql ="select * from person where idcard=?";
		Object[] params = {idcard};
		List<Person> list = new ArrayList<Person>();
		return super.queryAll(sql, params,page,size);
	}
	public List<Person> queryAll()throws SQLException{
		String sql ="select * from person";
		return super.queryAll(sql, null);
	}
	public PageBean queryAll(int page,int size)throws SQLException{
		String sql ="select * from person";
		return super.queryAll(sql, null, page, size);
	}
	public PageBean queryByFamilycode(String familycode,int page,int size)throws SQLException{
		String sql ="select * from person where familycode=?";
		Object[]params={familycode};
		return super.queryAll(sql, params, page, size);
	}
	public PageBean queryPersonByName(String fullname,int page,int size)throws SQLException{//查询名称查询户主
		String sql ="select * from person where fullname=?";
		Object[]params={fullname};
		return super.queryAll(sql, params, page, size);
	}
	
	public void update(Person p)throws SQLException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		try {
			DbUtil db = new DbUtil();
			conn=db.getConn();
			String sql = "update person set familycode=?,fullname=?,idcard=?,sex=?,nation=?,health=?,culture=?,attribute=?,farmer=?,marry=?,workunit=?,tel=? where ybcardno=?";
			pstmt=conn.prepareStatement(sql);
			int index=1;
			pstmt.setString(index++, p.getFamily().getFamilycode());
			pstmt.setString(index++, p.getFullname());
			pstmt.setString(index++, p.getIdcard());
			pstmt.setString(index++, p.getSex());
			pstmt.setString(index++, p.getNation());
			pstmt.setString(index++, p.getHealth());
			pstmt.setString(index++, p.getCulture());
			pstmt.setString(index++, p.getAttribute());
			pstmt.setString(index++, p.getFarmer());
			pstmt.setString(index++, p.getMarry());
			pstmt.setString(index++, p.getWorkunit());
			pstmt.setString(index++, p.getTel());
			pstmt.setString(index++, p.getYbcardno());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("修改参合农民档案失败 " + e.getMessage(), e);
		}
		finally{
			pstmt.close();
			conn.close();
		}
	}
	
	/*供参合缴费模块使用*/
	public PageBean queryAllHolder(int page,int size)throws SQLException{//查询所有身份为户主的参合农民
		String sql ="select * from person where relative='户主'";
		return super.queryAll(sql, null, page, size);
	}
	public PageBean queryHolderByName(String fullname,int page,int size)throws SQLException{//查询名称查询户主
		String sql ="select * from person where relative='户主' and fullname=?";
		Object[]params={fullname};
		return super.queryAll(sql, params, page, size);
	}
	
	
	public static void main(String[]args) throws Exception{
		/*
		Family f = new Family("一般农户", 4, 4, "450421", "45042101", "4504210101", "龙尾岗", "123");	
		Person p = new Person("ljj", "450421199807030519", "儿", "男", "汉", "jk", "bk", "ss", "yes", "no", "gxuwz", "15907741682");
		p.setFamily(f);
		p.add();*/
		
	}
}
